package com.media.themovieworld.screen.detail;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.media.themovieworld.R;
import com.media.themovieworld.model.PageItemInfo;


public class MovieDetailPagerController {

	private MovieDetailScreen mFrag;
	private ArrayList<PageItemInfo> mListPageInfo;
	
	public MovieDetailPagerController(MovieDetailScreen frag){
		mFrag = frag;
	}
	
	public FragmentPagerAdapter getDetailPagerAdapter(){
		loadListPageInfo();
//		ViewPagerIndicatorAdapter adapter = new ViewPagerIndicatorAdapter(mFrag.getFragmentManager(), mListPageInfo);
		ViewPagerDetailAdapter adapter = new ViewPagerDetailAdapter(mFrag.getFragmentManager(), mListPageInfo);
		return adapter;
	}
	
	/**
	 * Load list page info.
	 */
	private void loadListPageInfo(){
		mListPageInfo = new ArrayList<PageItemInfo>();
		PageItemInfo watchMoviePagerInfo = new PageItemInfo();
		watchMoviePagerInfo.pageTitle = mFrag.getResources().getString(R.string.play);
		watchMoviePagerInfo.fragClass = WatchMoviePagerScreen.class;
		
		PageItemInfo moviePagerInfo = new PageItemInfo();
		moviePagerInfo.pageTitle = mFrag.getResources().getString(R.string.movie_info);
		moviePagerInfo.fragClass = MoviePagerInfoScreen.class;
		
		PageItemInfo movieSuggestPagerInfo = new PageItemInfo();
		movieSuggestPagerInfo.pageTitle = mFrag.getResources().getString(R.string.suggest_movies);
		movieSuggestPagerInfo.fragClass = MovieSuggestPagerScreen.class;
		
		mListPageInfo.add(watchMoviePagerInfo);
		mListPageInfo.add(moviePagerInfo);
		mListPageInfo.add(movieSuggestPagerInfo);
	}
	
	public class ViewPagerDetailAdapter extends FragmentPagerAdapter{
		
		private ArrayList<PageItemInfo> mListPageInfo;
		
		public ViewPagerDetailAdapter(FragmentManager fm,ArrayList<PageItemInfo> listPageInfo) {
	        super(fm);
	        mListPageInfo = listPageInfo;
	        if (mListPageInfo == null) {
				mListPageInfo = new ArrayList<PageItemInfo>();
			}
	    }

	    /* (non-Javadoc)
		 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
		 */
		@Override
	    public Fragment getItem(int position) {
	    	Fragment frag = null;
			try {
				frag = (Fragment) mListPageInfo.get(position).fragClass.newInstance();
			} catch (java.lang.InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
	        return frag;
	    }

	    /* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#getCount()
		 */
		@Override
	    public int getCount() {
	        return mListPageInfo.size();
	    }

	    /* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#getPageTitle(int)
		 */
		@Override
	    public CharSequence getPageTitle(int position) {
	      return mListPageInfo.get(position).pageTitle;
	    }
	}

}
